package com.hellobike.haxing.modules.wms.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.modules.shop.entity.Shop;
import com.hellobike.haxing.modules.shop.service.IShopService;
import com.hellobike.haxing.modules.supplier.entity.TSupplierInfo;
import com.hellobike.haxing.modules.supplier.service.ITSupplierInfoService;
import com.hellobike.haxing.modules.wms.dto.ReturnSupplierDetailDto;
import com.hellobike.haxing.modules.wms.entity.ReturnSupplierDetail;
import com.hellobike.haxing.modules.wms.entity.WarehouseShop;
import com.hellobike.haxing.modules.wms.enums.ReturnStatusEnum;
import com.hellobike.haxing.modules.wms.facade.WmsReturnFacade;
import com.hellobike.haxing.modules.wms.service.IReturnSupplierDetailService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.modules.wms.service.IWarehouseShopService;
import com.hellobike.haxing.modules.wms.utils.ListUtil;
import lombok.extern.slf4j.Slf4j;

import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import com.hellobike.haxing.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import com.hellobike.haxing.common.annotation.AutoLog;
 /**
 * @Description: t_wms_return_supplier_detail
 * @Author: haxing-boot
 * @Date:   2020-02-11
 * @Version: V1.0
 */
@RestController
@RequestMapping("/wms/returnSupplierDetail")
@Slf4j
public class ReturnSupplierDetailController extends WmsBaseController<ReturnSupplierDetail, IReturnSupplierDetailService> {
	@Autowired
	private IReturnSupplierDetailService returnSupplierDetailService;
	
	/**
	 * 分页列表查询
	 *
	 * @param returnSupplierDetail
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_detail-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ReturnSupplierDetail returnSupplierDetail,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		returnSupplierDetail.setReturnStatus(ReturnStatusEnum.INIT.getValue());//只查询初始化的
		QueryWrapper<ReturnSupplierDetail> queryWrapper = QueryGenerator.initQueryWrapper(returnSupplierDetail, req.getParameterMap());
		Page<ReturnSupplierDetail> page = new Page<ReturnSupplierDetail>(pageNo, pageSize);
		IPage<ReturnSupplierDetail> pageList = returnSupplierDetailService.page(page, queryWrapper);
		IPage<ReturnSupplierDetailDto> result = wmsReturnFacade.wrapperPageSupplierDetail(pageList);
		return Result.ok(result);
	}
	
	/**
	 *   添加
	 *
	 * @param returnSupplierDetail
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_detail-添加",operateType=2)
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ReturnSupplierDetail returnSupplierDetail) {
		returnSupplierDetailService.save(returnSupplierDetail);
		return Result.ok("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param returnSupplierDetail
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_detail-编辑",operateType=3)
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ReturnSupplierDetail returnSupplierDetail) {
		returnSupplierDetailService.updateById(returnSupplierDetail);
		return Result.ok("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_detail-删除",operateType=4)
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		returnSupplierDetailService.removeById(id);
		return Result.ok("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "t_wms_return_supplier_detail-批量删除",operateType=4)
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.returnSupplierDetailService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ReturnSupplierDetail returnSupplierDetail = returnSupplierDetailService.getById(id);
		if(returnSupplierDetail==null) {
			return Result.error("未找到对应数据");
		}
		return Result.ok(returnSupplierDetail);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param returnSupplierDetail
    */
    @AutoLog(value = "t_wms_return_supplier_detail-导出excel",operateType=6)
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ReturnSupplierDetail returnSupplierDetail) {
        return super.exportXls(request, returnSupplierDetail, ReturnSupplierDetail.class, "t_wms_return_supplier_detail");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @AutoLog(value = "t_wms_return_supplier_detail-导入excel",operateType=5)
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ReturnSupplierDetail.class);
    }


    @Autowired
	private ITSupplierInfoService supplierInfoService;
	 @Autowired
	 private IWarehouseShopService warehouseShopService;
	 @Autowired
	 private IShopService shopService;
	 @Autowired
	 private WmsReturnFacade wmsReturnFacade;

	 @GetMapping(value = "/getShopsAndSuppliers")
	 public Result<?> getShopsAndSuppliers() {
		Map<String,Object> result = new HashMap<>();
		 List<Shop> shops = new ArrayList<>();
		 WarehouseShop param =new WarehouseShop();
		 param.setWarehouseId(getWarehouseId());
		 List<WarehouseShop> warehouseShops = warehouseShopService.getBaseMapper().selectList(new QueryWrapper<>(param));
		 if (ListUtil.isNotEmpty(warehouseShops)) {
			 for (WarehouseShop warehouseShop : warehouseShops) {
				 Shop shop = shopService.getById(warehouseShop.getShopId());
				 if(null != shop){
					 shops.add(shop);
				 }
			 }
		 }
		 List<TSupplierInfo> suppliers = supplierInfoService.getBaseMapper().selectList(new QueryWrapper<>());
		 result.put("shops",shops);
		 result.put("suppliers",suppliers);
		 return Result.ok(result);
	 }

}
